package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.AddressBook;
import com.itheima.domain.User;
import com.itheima.service.AddressBookService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 根据id查询地址列表
     *
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R addressId(HttpSession session) {
        //根据用户id查询地址列表数据
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //获取id
        Long userId = (Long) session.getAttribute("user");
        //按照id进行判断
        wrapper.eq(AddressBook::getUserId, userId);
        //查询地址id信息
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 新增
     *
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R addressSave(@RequestBody AddressBook addressBook,
                         HttpSession session) {
        //设置地址是谁的
        Long id = (Long) session.getAttribute("user");
        addressBook.setUserId(id);

        addressBookService.save(addressBook);
        return R.success("地址保存成功");
    }


    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R addressDefault(@RequestBody AddressBook addressBook) {

        //拿到id值
//        Long id = addressBook.getId();

        //把当前用户所有的地址的 默认值,设置为 0
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        AddressBook book = new AddressBook();
        book.setIsDefault(0);
        addressBookService.update(book, wrapper);

//        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.set(AddressBook::getIsDefault,0);
//        wrapper.eq(AddressBook::getUserId,id);
//        addressBookService.update(wrapper);

        //设置当前地址值 默认值为 1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);


        return R.success("设置成功");
    }


    /**
     * 代码回写
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R addressBackWiter(@PathVariable Long id) {
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }


    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R addressDelect(@RequestParam List<Long> ids) {
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改病保存
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R addressSave(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


    /**
     * 当前用户默认的收货地址
     *
     * @return
     */
    @GetMapping("/default")
    public R Defaultaddress() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //获取用户id
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //写入默认值
        wrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }


}